<?php
/*
Plugin Name: Daiko's Google VideoBar
Plugin URI: http://www.daikos.net/daikos-google-videobar/
Description: Adds a Google VideoBar to your sidebar. With a pop-up player.
Author: Rune Fjellheim
Version: 1.2
License: GPL
Author URI: http://www.daikos.net
*/


function widget_gsvideobar_init() {

	if ( !function_exists('register_sidebar_widget') || !function_exists('register_widget_control') )
		return;

	function widget_gsvideobar_control() {
			$options = $newoptions = get_option('widget_gsvideobar');
			if ( $_POST["gsvideobar-submit"] ) {
				$newoptions['title'] = strip_tags(stripslashes($_POST["gsvideobar-title"]));
				$newoptions['googleapi'] = strip_tags(stripslashes($_POST["gsvideobar-googleapi"]));
				$newoptions['resultset'] = strip_tags(stripslashes($_POST["gsvideobar-resultset"]));
				$newoptions['thumbnailsize'] = strip_tags(stripslashes($_POST["gsvideobar-thumbnailsize"]));
				$newoptions['horisontal'] = strip_tags(stripslashes($_POST["gsvideobar-horisontal"]));
				$newoptions['closetext'] = stripslashes($_POST["gsvideobar-closetext"]);
				$newoptions['search'] = strip_tags(stripslashes($_POST["gsvideobar-search"]));
				$newoptions['show'] = $_POST["gsvideobar-show"];
				$newoptions['slug'] = strip_tags(stripslashes($_POST["gsvideobar-slug"]));
				$newoptions['searchform'] = $_POST["gsvideobar-searchform"];
			}
			if ( $options != $newoptions ) {
				$options = $newoptions;
				update_option('widget_gsvideobar', $options);
			}
			$allSelected = $homeSelected = $postSelected = $pageSelected = $categorySelected = false;
			switch ($options['show']) {
				case "all":
				$allSelected = true;
				break;
				case "":
				$allSelected = true;
				break;
				case "home":
				$homeSelected = true;
				break;
				case "post":
				$postSelected = true;
				break;
				case "page":
				$pageSelected = true;
				break;
				case "category":
				$categorySelected = true;
				break;
			}
			$resultsetLarge = ($options['resultset']=="large");    
			$thumbnailSmall = ($options['thumbnailsize']=="small");
			$horisontal = ($options['horisontal']=="true");
			$AddSearchForm =  ($options['searchform']=="AddSearchForm");
		?>
			<label for="gsvideobar-title" title="Title above the widget" style="line-height:35px;display:block;">Title: <input type="text" style="width: 442px;" id="gsvideobar-title" name="gsvideobar-title" value="<?php echo htmlspecialchars($options['title']); ?>" /></label>
			<label for="gsvideobar-googleapi" title="Insert your Google Search API" style="line-height:35px;display:block;">Google API <a href="http://code.google.com/apis/ajaxsearch/signup.html">?</a>: <input type="text" style="width: 390px;" id="gsvideobar-googleapi" name="gsvideobar-googleapi" value="<?php echo htmlspecialchars($options['googleapi']); ?>" /></label>
			<table border="0px">
			<tr><td><label for="gsvideobar-resultset"  title="Resultset - Maximum 4 or Maximum 8:" style="line-height:25px;display:block;">Resultset: </td><td><input type="radio" name="gsvideobar-resultset" value="large" <?php if ($resultsetLarge==true){echo "checked";} ?>> Large </td><td><input type="radio" name="gsvideobar-resultset" value="small" <?php if ($resultsetLarge==false){echo "checked";} ?>> Small </label></td></tr>
			<tr><td><label for="gsvideobar-thumbnailsize"  title="Set thumbnail size:" style="line-height:25px;display:block;">Thumbnail size: </td><td><input type="radio" name="gsvideobar-thumbnailsize" value="small" <?php if ($thumbnailSmall==true){echo "checked";} ?>> Small </td><td><input type="radio" name="gsvideobar-thumbnailsize" value="medium" <?php if ($thumbnailSmall==false){echo "checked";} ?>> Medium </label></td></tr>
			<tr><td><label for="gsvideobar-horisontal"  title="Set orientation:" style="line-height:25px;display:block;">Orientation: </td><td><input type="radio" name="gsvideobar-horisontal" value="true" <?php if ($horisontal==true){echo "checked";} ?>> Horisontal </td><td><input type="radio" name="gsvideobar-horisontal" value="false" <?php if ($horisontal==false){echo "checked";} ?>> Vertical </label></td></tr>
			</table>
			<label for="gsvideobar-search" title="Insert your search terms separated by Line Brake. The options marked with * are optional. IMPORTANT: No Line Break after the last search term!">Search term(s) [SEARCH TERM(S)]@[SEARCH NAME*]<textarea style="width: 470px; height: 185px;" id="gsvideobar-search" name="gsvideobar-search"><?php echo htmlspecialchars($options['search']); ?></textarea></label>
			<label for="gsvideobar-closetext" title="Insert closing text, (Optional, standard is: I'm done watching this!)" style="line-height:35px;display:block;">Close Text: <input type="text" style="width: 402px;" id="gsvideobar-closetext" name="gsvideobar-closetext" value="<?php echo htmlspecialchars($options['closetext']); ?>" /></label>
			<label for="gsvideobar-show"  title="Show only on specified page(s)/post(s)/category. Default is All" style="line-height:35px;">Display only on: <select name="gsvideobar-show" id="gsvideobar-show"><option label="All" value="all" <?php if ($allSelected){echo "selected";} ?>>All</option><option label="Home" value="home" <?php if ($homeSelected){echo "selected";} ?>>Home</option><option label="Post" value="post" <?php if ($postSelected){echo "selected";} ?>>Post(s)</option><option label="Page" value="page" <?php if ($pageSelected){echo "selected";} ?>>Page(s)</option><option label="Category" value="category" <?php if ($categorySelected){echo "selected";} ?>>Category</option></select></label> 
			<label for="gsvideobar-slug"  title="Optional limitation to specific page, post or category. Use ID, slug or title." style="line-height:35px;">Slug/Title/ID: <input type="text" style="width: 180px;" id="gsvideobar-slug" name="gsvideobar-slug" value="<?php echo htmlspecialchars($options['slug']); ?>" /></label>
			<label for="gsvideobar-searchform"  title="Add a Search Form" style="line-height:25px;display:block;"> Add Search Form: <input type="checkbox" name="gsvideobar-searchform" value="AddSearchForm" <?php if ($AddSearchForm==true){echo "checked";} ?>></label><label for="gsvideobar-help" title="You can get more help and instructions on www.daikos.net!" style="line-height:25px;"><a href="http://www.daikos.net/daikos-google-videobar/">Help</a></label>
			<input type="hidden" name="gsvideobar-submit" id="gsvideobar-submit" value="1" />
		<?php
		}

	function widget_gsvideobar($args) { 
		extract($args);
		$options = get_option('widget_gsvideobar');
		$title = $options['title'];
		$searches = $options['search'];
		$searches = explode("\n", $searches);
		$totalsearches = count($searches)-1;
		$show = $options['show'];
		$slug = $options['slug'];
		$linkstart = '<a ';
		$linkstart_2 = '" href=\'javascript:dgvb.execute("';
		$linkmiddle =  '");\'>';
		$linkend ='</a>';
		$linktext ='<p>';
		$AddSearchForm =  ($options['searchform']=="AddSearchForm");
		 
        for ($i = 0; $i <= $totalsearches; $i++) {
			$pieces = explode("@", $searches[$i]);
			if ($pieces[1]==''){if($i < $totalsearches){$linkvalue=substr($pieces[0], 0, -1);} else{$linkvalue=$pieces[0];}} else {if($i < $totalsearches){$linkvalue=substr($pieces[1], 0, -1);} else{$linkvalue=$pieces[1];}}
			if ($i < $totalsearches){$separator=" | ";} else {$separator = "";}
			$searchterm = addslashes($pieces[0]); 
			$searchterm = str_replace("&#8220;", "%27", $searchterm);
			$searchterm = str_replace("&#8221;", "%27", $searchterm);
			$searchterm = str_replace('\'', '%27', $searchterm);
			$searchterm = str_replace('\'', '%27', $searchterm);
			$linkclass = 'class="searchlink-'.$i;
			$linktext = $linktext.$linkstart.$linkclass.$linkstart_2.$searchterm.$linkmiddle.$linkvalue.$linkend.$separator;
		} 
		$linktext = str_replace("\n", "", $linktext);
		$videoinfo = "</p><div id='DaikosGSVideoBar'>Loading...</div><p><small><a href='http://www.daikos.net' title='Daiko&#8217;s Google VideoBar 1.2'>Widgetized by Daiko</a></small></p>";
		if ($AddSearchForm){
			$search_form = '<form action="" onsubmit="return formdgvb(this.q.value,dgvb)"><input type="text" name="q" style="width: 73%;" /><input type="submit" value="Go" style="padding: 2px;" /></form>';
		}
		else {$search_form='';}
		$fulltext = $search_form.$linktext.$videoinfo;
        
		echo "<!-- Daiko's Google VideoBar START -->\n";
		echo $before_widget; 
        echo "<div class='DaikosGoogleVideoBar'>"; 
   		switch ($show) {
				case "all": 
					$title ? print($before_title . $title . $after_title) : null;
                	echo $fulltext;
				break;
				case "home":
				if (is_home()) {
					$title ? print($before_title . $title . $after_title) : null;
                	echo $fulltext;
		  		}
          		else {
            		echo "<!-- Daiko's Google VideoBar is disabled for this page/post! -->";
          		}
				break;
				case "post":
				if (is_single($slug)) {
					$title ? print($before_title . $title . $after_title) : null;
                	echo $fulltext;
		  		}
          		else {
            		echo "<!-- Daiko's Google VideoBar is disabled for this page/post! -->";
          		}
				break;
				case "page":
				if (is_page($slug)) {
					$title ? print($before_title . $title . $after_title) : null;
                	echo $fulltext;
		  		}
          		else {
            		echo "<!-- Daiko's Google VideoBar is disabled for this page/post! -->";
          		}
				break;
				case "category":
				if (is_category($slug)) {
					$title ? print($before_title . $title . $after_title) : null;
                	echo $fulltext;
		  		}
          		else {
            		echo "<!-- Daiko's Google VideoBar is disabled for this page/post! -->";
          		}
				break;
								
			}
		echo "</div>"; 
		echo $after_widget; 
		echo "\n<!-- Daiko's Google VideoBar END -->";
		?>
	
<?php
	}

 	function widget_gsvideobar_head() {
		$options = get_option('widget_gsvideobar');
		$show = $options['show'];
		$slug = $options['slug'];
		$searches = $options['search'];
		$searches = explode("\n", $searches);
		$rndm =   mt_rand(0, count($searches) - 1 );
		$search = wptexturize( $searches[$rndm] );
		$pieces = explode("@", $search);
		$searchterm = addslashes($pieces[0]);
		$searchterm = str_replace("&#8220;", "'", $searchterm);
		$searchterm = str_replace("&#8221;", "'", $searchterm);
		$searchterm = str_replace("&#8216;", "'", $searchterm);
		$searchterm = str_replace("&#8217;", "'", $searchterm);
		$resultsetLarge = ($options['resultset']=="large");
		$resultset ='false';
		$horisontal ='';
		if($options['horisontal']=='true') {$horisontal='horizontal : true,';}
		if ($resultsetLarge){$resultset='true';}    
		$thumbnailSmall = ($options['thumbnailsize']=="small");
		$thumbnail = 'THUMBNAILS_MEDIUM';
		if ($thumbnailSmall){$thumbnail = 'THUMBNAILS_SMALL';}
		echo "<!-- Daiko's Google VideoBar Header START -->\n";
   		switch ($show) {
				case "all": 
					$doshow = true;
				break;
				case "home":
				if (is_home()) {
					$doshow = true;
		  		}
          		else {
					$doshow = false;
            		echo "<!-- Daiko's Google VideoBar is disabled for this page/post! -->";
          		}
				break;
				case "post":
				if (is_single($slug)) {
					$doshow = true;
		  		}
          		else {
 					$doshow = false;
           			echo "<!-- Daiko's Google VideoBar is disabled for this page/post! -->";
          		}
				break;
				case "page":
				if (is_page($slug)) {
					$doshow = true;
		  		}
          		else {
					$doshow = false;
            		echo "<!-- Daiko's Google VideoBar is disabled for this page/post! -->";
          		}
				break;
				case "category":
				if (is_category($slug)) {
					$doshow = true;
		  		}
          		else {
					$doshow = false;
            		echo "<!-- Daiko's Google VideoBar is disabled for this page/post! -->";
          		}
				break;
								
			}
		
		if ($doshow) {
	?>
	<!--[if lt IE 7]>
		<script src="<?php bloginfo('url'); ?>/wp-content/plugins/widgets/daikosiefix/fixed.js" type="text/javascript"></script>
	<![endif]-->
	<script src="http://www.google.com/uds/api?file=uds.js&amp;v=1.0&amp;key=<?php echo $options['googleapi'] ?>" type="text/javascript"></script>
	<link href="http://www.google.com/uds/css/gsearch.css" rel="stylesheet" type="text/css"/>
	<script type="text/javascript">//<![CDATA[
		var scriptStr = 'unknown.js';
		if (typeof document.attachEvent != 'undefined') scriptStr = '<?php bloginfo('url'); ?>/wp-content/plugins/widgets/daikosiefix/gsvideobar_daiko.js';
		if (typeof document.addEventListener != 'undefined') scriptStr = 'http://www.google.com/uds/solutions/videobar/gsvideobar.js';
		document.write("<scr"+"ipt type='text/javascript' src='"+scriptStr+"'></scr"+"ipt>");
    	//]]>
	</script>
	<link href="http://www.google.com/uds/solutions/videobar/gsvideobar.css" rel="stylesheet" type="text/css"/>
	<script type="text/javascript">//<![CDATA[
	    var dgvb;
		function formdgvb(q,videobar) {
			if (q && q!= "") {
				videobar.execute(q);
			}
				return false;
		}
	
	    function DGVBLoad() {
	    	var barContainer = document.getElementById("DaikosGSVideoBar");
	    	var playerContainer = document.getElementById("videoPlayer");

			var options = {
			<?php echo $horisontal; ?>
			largeResultSet : <?php echo $resultset; ?>,
			thumbnailSize : GSvideoBar.<?php echo $thumbnail; ?>,
			string_allDone : "<?php if ($options['closetext'] != ''){echo $options['closetext'];} else {echo 'I\'m done watching this';} ?>" 
			}

	      dgvb = new GSvideoBar(barContainer, playerContainer, options);
	      dgvb.execute("<?php echo $searchterm; ?>");
	    } 
		//]]>
        </script>

	<style type="text/css">
		.searchlink-<?php echo $rndm; ?> {font-weight:bold;}
		.DaikosGoogleVideoBar p {text-align:center;}
		.alldone_gsvb {color:black;}
		.playerInnerBox_gsvb .player_gsvb {width : 480px;height : 380px;margin : 20px;z-index:100;}
		#videoPlayer {display:block; top:100px; left:0px; width:100%; position:fixed; background:#FBFBFB ;text-align:center; font-weight:bold;z-index:100;}
		#videoPlayer a {color:black;}
		#videoPlayer {filter:alpha(opacity=90);filter:progid:DXImageTransform.Microsoft.Alpha(opacity=90); -moz-opacity: 0.90; opacity:0.9;}
	</style>

	<?php 
		}
	echo "\n<!-- Daiko's Google VideoBar Header END -->";

	}	

	function widget_gsvideobar_footer() {
		$options = get_option('widget_gsvideobar');
		$show = $options['show'];
		$slug = $options['slug'];
    	echo "\n<!-- Daiko's Google VideoBar Footer START -->\n";
		switch ($show) {
			case "all": 
				$doshow = true;
			break;
			case "home":
			if (is_home()) {
				$doshow = true;
	  		}
      		else {
				$doshow = false;
        		echo "<!-- Daiko's Google VideoBar is disabled for this page/post! -->";
      		}
			break;
			case "post":
			if (is_single($slug)) {
				$doshow = true;
	  		}
      		else {
				$doshow = false;
       			echo "<!-- Daiko's Google VideoBar is disabled for this page/post! -->";
      		}
			break;
			case "page":
			if (is_page($slug)) {
				$doshow = true;
	  		}
      		else {
				$doshow = false;
        		echo "<!-- Daiko's Google VideoBar is disabled for this page/post! -->";
      		}
			break;
			case "category":
			if (is_category($slug)) {
				$doshow = true;
	  		}
      		else {
				$doshow = false;
        		echo "<!-- Daiko's Google VideoBar is disabled for this page/post! -->";
      		}
			break;
							
		}
	
		if ($doshow) {
	?>
	<div id='videoPlayer'>Loading...</div>
	<script type="text/javascript">DGVBLoad();</script>
	<?php
	}
	echo "\n<!-- Daiko's Google VideoBar Footer END -->\n";
	}	

	register_sidebar_widget('Daiko\'s Google VideoBar', 'widget_gsvideobar');
	register_widget_control('Daiko\'s Google VideoBar', 'widget_gsvideobar_control', 490, 510);
	if ( is_active_widget('widget_gsvideobar') )
		add_action('wp_head', 'widget_gsvideobar_head'); 
		add_action('wp_footer', 'widget_gsvideobar_footer');
	
}

add_action('plugins_loaded', 'widget_gsvideobar_init'); 

?>